package com.hw01.common;

import java.util.List;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;

/**
 * 分页响应类
 */
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain = true)
public class ResultPage<T> {

    private Long total;
    private Long offset;
    private Long size;
    private Boolean queryState = true;
    private String msg;
    private List<T> data;

    /**
     * 从结果page转成wrap
     *
     * @param page 查询结果
     * @return
     */
    public static <R> ResultPage<R> fromPage(IPage<R> page) {
        ResultPage<R> pageWrap = new ResultPage<>();
        pageWrap.data = page.getRecords();
        pageWrap.offset = page.offset();
        pageWrap.total = page.getTotal();
        return pageWrap;
    }

    /**
     * 转换成查询类page
     *
     * @param pageQuery 分页参数
     * @return
     */
    public static <R> Page<R> toQueryPage(PageQuery pageQuery) {
        if (pageQuery == null) {
            pageQuery = new PageQuery();
        }

        Long limit = pageQuery.getLimit();
        long currentPage = pageQuery.getPage();

        return new Page<>(currentPage, limit);
    }
}
